﻿/************************************************************************
* Copyright (c) 2018 All Rights Reserved.
*命名空间：AOP.Filter.Exception
*文件名： AopExceptionFilterAttribute
*创建人： kingling
*创建时间：2018/7/5 11:19:58
*描述
*=======================================================================
*修改标记
*修改时间：2018/7/5 11:19:58
*修改人：kingling
*描述：
************************************************************************/

using Common.Standard;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace AOP.Filter.Exception
{
    public class AopExceptionFilterAttribute : ExceptionFilterAttribute
    {
        /// <summary>
        /// 异常发生
        /// </summary>
        /// <param name="context"></param>
        public override void OnException(ExceptionContext context)

        {
            var type = context.Exception.GetType();
            var msg = new Msg
            {
                code = 1,
                err = "系统出错"
            };
            if (type.FullName.EndsWith("Error"))
            {
                msg.err = context.Exception.Message;
            }
            else
            {
                var ex = context.Exception;
                while (ex != null)
                {
                    LogerHelper.Error(ex.Message);
                    LogerHelper.Error("堆栈:" + ex.StackTrace);
                    LogerHelper.Error("##########################################################################");
                    ex = ex.InnerException;
                }
            }
            var result = new JsonResult(msg);
            context.Result = result;
            base.OnException(context);
        }
    }
}
